package com.ctg.itrdc.sysmgr.portal.control;

import java.util.List;

import javax.annotation.Resource;
import javax.validation.Valid;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.ctg.itrdc.pasp.security.SecUser;
import com.ctg.itrdc.pasp.security.utils.WebUtils;
import com.ctg.itrdc.sysmgr.portal.api.dto.OrganizationDTO;
import com.ctg.itrdc.sysmgr.portal.api.service.IOrganizationService;
import com.ctg.itrdc.sysmgr.portal.common.entity.CommonResult;
import com.ctg.itrdc.sysmgr.portal.utils.DTOHelper;
import com.google.common.collect.Lists;

@Controller
@RequestMapping("/organization")
public class OrganizationController extends
		SysmgrCURDController<Long, OrganizationDTO> {

	@Resource
	private IOrganizationService organizationService;

	/**
	 * @api {post} /organization/insert 新增记录
	 * @apiGroup organization
	 * @apiName insert
	 * @apiParam {String} orgCode 电信组织编码 可为空
	 * @apiParam {String} orgName 电信组织名称 可为空
	 * @apiParam {Long} commonRegionId 公用管理区域标识,记录区域唯一标识 可为空
	 * @apiParam {Long} parentOrgId 上级组织标识,直接记录组织的直接管理上级标识 可为空
	 * @apiParam {String} statusCd 电信组织状态 可为空
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/insert")
	@ResponseBody
	public CommonResult insert(@Valid OrganizationDTO dto, BindingResult result)
			throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		return new CommonResult(HttpStatus.OK.toString(), "新增成功",
				organizationService.insert(dto));
	}
	
	@Override
	public CommonResult updateByPrimaryKey(OrganizationDTO dto, BindingResult result) throws Exception {
		DTOHelper.valid(result);
		DTOHelper.updateDTO(dto);
		int i = organizationService.updateByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "更新数据成功",i);
	}

	// @RequestMapping("/tree")
	// @ResponseBody
	// public CommonResult selectOrgTree(
	// @RequestParam(required = false) Long sysPostId,
	// OrganizationDTO dto
	// ){
	// if (sysPostId == null) {
	// CtgUser ctgUser = WebUtils.getCtgUser();
	// sysPostId = ctgUser.getSysPostId();
	// }
	// List<OrganizationDTO> dtoList =
	// organizationService.selectOrgTree(sysPostId);
	// return new CommonResult(HttpStatus.OK.toString(),"success",dtoList);
	// }


	/**
	 * @api {get} /organization/sub 查询子组织
	 * @apiGroup organization
	 * @apiName sub
	 * @apiParam {Long} orgId 选填 任职组织id,不填则从当前登录用户中获取
	 * @apiParam {Long} parentOrgId 选填 父级组织id
	 * @apiParam {String} orgName 选填 搜索时使用，同时传入orgId和orgName,进行模糊搜索
	 * @apiDescription 异步组织树使用方法：1.顶级节点，传入任职组织id（orgId）,默认从当前登录用户中获取<br/>
	 *                 2.展开下级组织，通过传入父级组织id（parentOrgId）获取
	 * @apiVersion 0.0.1
	 */
	@RequestMapping("/sub")
	@ResponseBody
	public CommonResult selectSubOrg(OrganizationDTO dto) {
		// 父级组织为空，则访问顶级组织
		List<OrganizationDTO> dtoList =null;
		if(dto.getParentOrgId()==null){
			SecUser ctgUser = WebUtils.getCtgUser();
			Long rzOrgId = ctgUser.getRzOrgId();
			dto.setOrgId(rzOrgId);
			dtoList = organizationService.selectSubOrg(dto);
			if(CollectionUtils.isNotEmpty(dtoList)){
				for (OrganizationDTO top : dtoList) {
					OrganizationDTO sub = new OrganizationDTO();
					sub.setStatusCd(top.getStatusCd());
					sub.setParentOrgId(top.getOrgId());
					List<OrganizationDTO> subList = organizationService.selectSubOrg(sub);
					top.setNodes(subList);
				}
			}
		}else{
			dtoList = organizationService.selectSubOrg(dto);
		}
		return new CommonResult(HttpStatus.OK.toString(), "success", dtoList);
	}

	@RequestMapping("/delete")
	@ResponseBody
	public CommonResult deleteByPrimaryKey(OrganizationDTO dto) throws Exception {
		int i = organizationService.deleteByPrimaryKey(dto);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功",i);
	}
	
	/**
	 * 批量删除，不推荐使用
	 * 
	 * @param ids
	 * @param dto
	 * @return
	 * @throws Exception
	 */
	@RequestMapping("/batch/delete")
	@ResponseBody
	public CommonResult batchDeleteByPrimaryKey(@RequestParam String ids, OrganizationDTO dto)
			throws Exception {
		List<OrganizationDTO> dtoList = Lists.newArrayList();
		String[] idStr = ids.split(",");
		for (String i : idStr) {
			long id = Long.parseLong(i);
			OrganizationDTO tmp = new OrganizationDTO();
			tmp.setOrgId(id);
			dtoList.add(tmp);
		}

		organizationService.batchDeleteByPrimaryKey(dtoList);
		return new CommonResult(HttpStatus.OK.toString(), "删除成功");
	}
}
